<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<section class="content-header">
    <h1>派工单-转序接收</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12" style="margin-top: 50px;margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group" style="margin-right: 50px;">
                    <label>用户名：[[${user.name}]]</label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>部门：[[${user.deptName}]]</label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>车间：[[${user.branchName}]]</label>
                </div>
            </div>
        </div>
        <div class="col-xs-12" style="margin-top: 20px;margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group">
                    <label for="select-prodNo">生产令编号</label>
                    <select id="select-prodNo" class="form-control" style="width: 140px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-special">类型</label>
                    <select id="select-special" class="form-control select2" style="width: 140px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-part">部分</label>
                    <select id="select-part" class="form-control select2" style="width: 200px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-drawNo">图号</label>
                    <select id="select-drawNo" class="form-control select2" style="width: 300px;text-align: center"></select>
                </div>
            </div>
        </div>
        <div class="col-xs-12" style="margin-top: 20px;margin-left: 2%;margin-right: 2%;">
            <form id="queryForm" onsubmit="return false;">
                <div class="form-inline">
                    <div class="form-group">
                        <label for="query-barCode">二维码区域</label>
                        <input id="query-barCode" type="text" name="barCode" class="form-control"
                               placeholder="请扫描装配派工单二维码"
                               style="width: 550px;margin-right: 10px;" autocomplete="off" onkeyup="loadData()">
                        <button type="button" class="btn btn-success" id="btn-search" style="margin-bottom: 0px;">
                            <span class="Bold">搜索</span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <hr style="margin-left: 2%;margin-right: 2%;margin-top: 30px;">
    <div class="row">
        <div class="col-xs-12" style="margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group" style="margin-right: 50px;">
                    <label>项目编号：<span id="spProCode"></span></label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>项目名称：<span id="spProName"></span></label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>部分：<span id="spPart"></span></label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>类型：<span id="special"></span></label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>装配图号：<span id="drawNo"></span></label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>装配名称：<span id="drawName"></span></label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>装配数量：<span id="drawNum"></span></label>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div style="margin-top: 15px;margin-left: 1%;margin-right: 1%;">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table;

    /**
     * 检测回车
     */
    function loadData() {
        let ev = window.event || e;
        let keyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
        if (keyCode == 13) {
            js.table.refresh(table);
            getAsseSheet();
        }
    }

    /** 接收 */
    function rec(id) {
        js.modal.confirm("确认转序接收吗？", function () {
            js.post({
                url: ctx + "assemble/receive/receive",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    /** 获取装配派工单信息 */
    function getAsseSheet() {
        js.post({
            url: ctx + "assemble/receive/asseSheet",
            data: {barCode: $("#query-barCode").val()},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    var rb = result.data;
                    $("#spProCode").text(rb.proCode);
                    $("#spProName").text(rb.proName);
                    $("#spPart").text(rb.part);
                    $("#special").text(rb.special);
                    $("#drawNo").text(rb.drawNo);
                    $("#drawName").text(rb.drawName);
                    $("#drawNum").text(rb.drawNum);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    $(function () {

        /** 生产令号下拉选 */
        js.post({
            url: ctx + "assemble/apply/getRecProdNoList",
            data: {},
            success: function (result) {
                $("#select-prodNo").html('').select2();
                let select2Html = '<option value=""></option>';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                $("#select-prodNo").html(select2Html).select2();
            }
        });
        /** 项目编号下拉框选中 */
        $("#select-prodNo").on("select2:select", function () {
            getSpecials();
        });
        /** 类型下拉框选中 */
        $("#select-special").on("select2:select", function () {
            getParts();
        });
        /** 部分下拉框选中 */
        $("#select-part").on("select2:select", function () {
            getDrawNos();
        });
        /** 图号下拉框选中 */
        $("#select-drawNo").on("select2:select", function () {
            if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                $("#query-barCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|2');
                getAsseSheet();
            } else {
                $("#query-barCode").val('');
            }
            js.table.refresh(table);
        });

        $('#bootstrap-table').bootstrapTable('destroy');//这里必须要添加这个销毁，否则新增、修改、查看的切换可编辑列表中的数据可能加载出现问题。
        /** 表单数据绑定 */
        table = js.table.init({
            url: ctx + "assemble/receive/receiveList",
            showExport: false,
            pagination: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "接收路线", field: 'route'},
                {title: "下序路线", field: 'nextRoute'},
                {title: "转序数量", field: 'num'},
                {title: "转出人", field: 'applyName'},
                {
                    title: "路线状态", field: 'status',
                    formatter: function (value, row) {
                        if (value === 3) {
                            return '<span class="badge label-inverse">部分转序</span>';
                        } else if (value === 4) {
                            return '<span class="badge label-success">全部转序</span>';
                        }
                    }
                },
                {title: "质检人", field: 'checkName'},
                {title: "质检结果", field: 'result'},
                {title: "质检时间", field: 'checkTime'},
                {
                    title: "操作", width: '90',
                    formatter: function (value, row, index) {
                        let actions = [];
                        if (hasP('asseOrd:receive:edit')) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="rec(' + row.id + ')"> 接收</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 点击查询 */
        $("#btn-search").click(function () {

            /** 校验二维码有效性 */
            var mm = $("#query-barCode").val().split("|");
            if (mm.length != 4) {
                js.modal.warning("二维码无效，请核对后再次扫描");
                return;
            }
            if (mm[3] != 2) {
                js.modal.warning("二维码无效，请核对后再次扫描");
                return;
            }
            getAsseSheet();
            js.table.search(table);
        });

    })

    /** 获取类型列表 */
    function getSpecials() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoSpecialList",
            data: {prodNo: $("#select-prodNo").val()},
            success: function (result) {
                $("#select-special").empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-special").html(select2Html).select2();
                    $("#select-special").val(result.data[0].id).select2();
                }
                getParts();
            }
        });
    }
    /** 获取部分列表 */
    function getParts() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoPartList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val()
            },
            success: function (result) {
                $("#select-part").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-part").html(select2Html).select2();
                    $("#select-part").val(result.data[0].id).select2();
                }
                getDrawNos();
            }
        });
    }
    /** 获取图号列表 */
    function getDrawNos() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoDrawNoList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val(),
                partId: $("#select-part").val()
            },
            success: function (result) {
                $("#select-drawNo").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-drawNo").html(select2Html).select2();
                    $("#select-drawNo").val(result.data[0].id).select2();
                }
                if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                    $("#query-barCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|2');
                    getAsseSheet();
                } else {
                    $("#query-barCode").val('');
                }
                js.table.refresh(table);
            }
        });
    }

</script>
</body>
</html>
